package com.cskaoyan.mall.promo.task;

import com.cskaoyan.mall.promo.service.PromoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author Pudding
 * @Date 2024/7/22 11:32
 */
@EnableScheduling
@Component
@Slf4j
public class ScheduledTask {

    @Autowired
    PromoService promoService;

    /**
     * 定时任务：初始化本地库存状态位
     */
    @Scheduled(cron = "0 0 2 * * ?")   // 每天凌晨2点执行
    public void initCacheTask(){

        log.info("定时任务：开始初始化本地库存状态位");
        promoService.importIntoCache();
        log.info("定时任务：结束初始化本地库存状态位");
    }


    /**
     * 定时任务：定时任务结束，清空redis缓存
     */
    @Scheduled(cron = "0 0 22 * * ?")  //每天晚上10点清除缓存
    public void clearRedisCache() {

        log.info("定时任务：开始清空redis缓存");
        promoService.clearRedisCache();
        log.info("定时任务：结束清空redis缓存");

    }
}
